/**
 * @author Haidar Osman
 */
var UMLCanvas = function(stage, name, parent, relationObjArray) {
	this.parent = parent;
	this.stage = stage;
	this.layer = new Kinetic.Layer();

	this.stage.add(this.layer);
	this.selected = null;
	this.classes = [];
	this.relations = new RelationArray(relationObjArray);
	this.name = name;
	// this.positions={};
	var that = this;
	this.backgroundRect = new Kinetic.Rect({
		x : 0, // - this.headerText.padding,
		y : 0, // - this.headerText.padding - this.headerText.fontSize,
		width : this.stage.width,
		height : this.stage.height,
		fill : styles.backgroundColor,
		stroke : "black",
		strokeWidth : 1,
		draggable : false
	});

	this.layer.add(this.backgroundRect);
	this.backgroundRect.moveToBottom();
	this.mouseX = 0;
	this.mouseY = 0;
	this.makingRelation = false;
	this.makingGeneralization = false;
	this.buildingRelation = new UnderConstructionRelation(0, 0, 15, 15);
	that.layer.add(that.buildingRelation.relationLine);
	that.buildingRelation.relationLine.visible = false;
	this.fromClass = null;

	this.addClass = function(newClass) {
		newClass.parentShape = that;
		newClass.group.on("mousedown", function() {
			that.selected = newClass;
			if(that.makingRelation) {
				//alert("making");
				that.addRelation(that.fromClass, newClass, "uni");
				that.backgroundRect.off("mousemove");
				that.makingRelation = false;
				that.buildingRelation.relationLine.visible = false;
				that.fromClass.group.draggable(true);
				newClass.update();
				that.layer.draw();
			} else if(that.makingGeneralization) {
				// alert("end");
				that.addRelation(that.fromClass, newClass, "generalization");
				that.backgroundRect.off("mousemove");
				that.makingGeneralization = false;
				that.buildingRelation.relationLine.visible = false;
				that.fromClass.group.draggable(true);
				newClass.update();
				that.layer.draw();
			}
		});
		newClass.rightRelationAnchor.on("click", function() {
			that.fromClass = newClass;
			// alert("start");
			newClass.group.draggable(false);
			that.makingRelation = true;
			that.buildingRelation.relationLine.points[0].x = newClass.rightRelationAnchor.getAbsolutePosition().x;
			that.buildingRelation.relationLine.points[0].y = newClass.rightRelationAnchor.getAbsolutePosition().y;
			that.backgroundRect.on("mousemove", function() {
				if(that.makingRelation) {
					that.buildingRelation.relationLine.points[1].x = that.stage.getMousePosition().x;
					that.buildingRelation.relationLine.points[1].y = that.stage.getMousePosition().y;
					that.buildingRelation.relationLine.visible = true;
					that.layer.draw();
				}
			});
		});

		newClass.generalizationAnchor.on("click", function() {
			that.fromClass = newClass;

			newClass.group.draggable(false);
			that.makingGeneralization = true;
			that.buildingRelation.relationLine.points[0].x = newClass.generalizationAnchor.getAbsolutePosition().x;
			that.buildingRelation.relationLine.points[0].y = newClass.generalizationAnchor.getAbsolutePosition().y;
			that.backgroundRect.on("mousemove", function() {
				if(that.makingGeneralization) {
					that.buildingRelation.relationLine.points[1].x = that.stage.getMousePosition().x;
					that.buildingRelation.relationLine.points[1].y = that.stage.getMousePosition().y;
					that.buildingRelation.relationLine.visible = true;
					that.layer.draw();
				}
			});
		});

		newClass.leftRelationAnchor.on("click", function() {
			that.fromClass = newClass;
			// alert("start");
			newClass.group.draggable(false);
			that.makingRelation = true;

			that.buildingRelation.relationLine.points[0].x = newClass.rightRelationAnchor.getAbsolutePosition().x;
			that.buildingRelation.relationLine.points[0].y = newClass.rightRelationAnchor.getAbsolutePosition().y;
			that.backgroundRect.on("mousemove", function() {
				if(that.makingRelation) {
					that.buildingRelation.relationLine.points[1].x = that.stage.getMousePosition().x;
					that.buildingRelation.relationLine.points[1].y = that.stage.getMousePosition().y;
					that.buildingRelation.relationLine.visible = true;
					that.layer.draw();
				}
			});
		});

		that.layer.add(newClass.group);
		newClass.group.moveToTop();
		that.classes[that.classes.length] = newClass;
		that.addClassRelations(newClass);
		newClass.update();
		that.update();
		//that.layer.draw();

	}

	this.backgroundRect.on("dblclick", function() {
		// alert("something");
		var mouseX = that.stage.getMousePosition().x + $('#container').prop("offsetLeft");
		var mouseY = that.stage.getMousePosition().y + $('#container').prop("offsetTop");
		that.mouseY = mouseY;
		that.mouseX = mouseX;
		// alert(that.mouseX+" "+that.mouseY);
		var formString = "<div id='addClassPopUp' style='position:absolute;top:" + mouseY + "px;left:" + mouseX + "px;z-index:30;'>" + "Enter the class Name" + "<br />" + "<input type='text' id='className' autofocus width='15'/>" + "</div>"
		if($('#className').length !== 0) {
			$('#addClassPopUp').remove();
		}
		$("#" + "canvasContainer").before(formString);
		$('input#className').focus();
		$('input#className').keydown(function(event) {
			if(event.which == 13) {
				var className = $('input#className').val();
				if(className !== "") {
					that.mouseX -= $('#canvasContainer').prop("offsetLeft");
					that.mouseY -= $('#canvasContainer').prop("offsetTop");
					var newClass = new ClassShape(that.mouseX, that.mouseY, new Class(className));
					that.parent.addClass(newClass);
				}
				$('#addClassPopUp').remove();
			} else if(event.which == 27) {
				$('#addClassPopUp').remove();
			}
		});
	});
	this.addClassObj = function(classObj) {
		if(this.getClass(classObj.name) == null) {
			var newClass = new ClassShape(0, 0, classObj);
			this.addClass(newClass);
		} else {
			alert("class is already in view")
		}
	}
	this.cancelClassForm = function() {
		$('#addClassPopUp').remove();
	}
	this.saveClassForm = function() {
		var newClass = new ClassShape(that.mouseX, that.mouseY, $('input#className').val(), "Calibri", 20, "black");
		that.addClass(newClass);
		$('#addClassPopUp').remove();
	}

	this.addRelation = function(fromClass, toClass, type) {
		that.removeRelation(fromClass, toClass);
		if(type == "uni") {
			var r = new UniRelation(fromClass, toClass, new RelationObj(fromClass.classObj, toClass.classObj, type));
		} else if(type == "generalization") {
			that.removeRelation(toClass, fromClass);
			var r = new GeneralizationRelation(fromClass, toClass, new RelationObj(fromClass.classObj, toClass.classObj, type));

		} else {
			var r = new Relation(fromClass, toClass, new RelationObj(fromClass.classObj, toClass.classObj, type));
		}
		that.relations.addRelation(r);
		that.layer.add(r.group);
		return r;
	}

	this.removeClass = function(className) {

		for( i = 0; i < that.classes.length; i++) {
			if(that.classes[i].classObj.name == className) {
				var j = 0;
				// for( j = 0; j < that.classes[i].outgoingRelations.members.length; j++) {
				// that.removeRelation(that.classes[i].outgoingRelations.members[j].fromClass, that.classes[i].outgoingRelations.members[j].toClass);
				// }
				// for( j = 0; j < that.classes[i].incomingRelations.members.length; j++) {
				// that.removeRelation(that.classes[i].incomingRelations.members[j].fromClass, that.classes[i].incomingRelations.members[j].toClass);
				// }
				that.layer.remove(that.classes[i].group);
				that.classes.splice(i, 1);
				//removeItem(className);
				break;
			}
		}
	}
	this.addClassRelations = function(newClass) {
		// var i=0;
		// for(i=0;i<newClass.outgoingRelations.members.length;i++)
		// {
		// if(that.getRelation(newClass,newClass.outgoingRelations.members[i].toClass)==null)
		// {
		// if(that.getClass(newClass.outgoingRelations.members[i].toClass.classObj.name)!==null)
		// {
		// that.relations[that.relations.length] = newClass.outgoingRelations.members[i];
		// }
		// }
		// }
		//
		// for(i=0;i<newClass.incomingRelations.members.length;i++)
		// {
		// if(that.getRelation(newClass.incomingRelations.members[i].fromClass,newClass)==null)
		// {
		// if(that.getClass(newClass.incomingRelations.members[i].fromClass.classObj.name)!==null)
		// that.relations[that.relations.length] = newClass.incomingRelations.members[i];
		// }
		// }
		// that.updateRelations();
	}
	this.getRelation = function(fromClass, toClass) {
		return that.relations.getRelation(fromClass, toClass);
	}
	this.removeRelation = function(fromClass, toClass) {
		var r = that.getRelation(fromClass, toClass);
		if(r != null) {
			that.layer.remove(r.group);
		}
		that.relations.removeRelation(fromClass, toClass);
	}

	this.getClass = function(className) {
		var i = 0;
		for( i = 0; i < that.classes.length; i++) {
			if(that.classes[i].classObj.name == className) {
				return that.classes[i];
			}
		}
		return null;
	}
	this.update = function() {
		var i = 0;
		this.backgroundRect.moveToBottom();
		for( i = 0; i < that.classes.length; i++) {

			that.classes[i].update();
			//alert(that.classes[i].headerRect.getAbsolutePosition().x);
		}
		that.layer.draw();
	}

	this.updateRelations = function() {
		var i = 0;
		for( i = 0; i < that.relations.members.length; i++) {
			this.layer.remove(that.relations.members[i].group);
		}
		this.relations.members = [];

		for( i = 0; i < that.relations.relationObjArray.members.length; i++) {
			// var fClass=that.parent
			var type = that.relations.relationObjArray.members[i].type;
			var fromClass = that.getClass(that.relations.relationObjArray.members[i].fromClass.name);
			var toClass = that.getClass(that.relations.relationObjArray.members[i].toClass.name);
			if((fromClass != null) && (toClass != null)) {
				if(type == "uni") {
					var r = new UniRelation(fromClass, toClass, that.relations.relationObjArray.members[i]);
				} else if(type == "generalization") {
					that.removeRelation(toClass, fromClass);
					var r = new GeneralizationRelation(fromClass, toClass, that.relations.relationObjArray.members[i]);

				} else {
					var r = new Relation(fromClass, toClass, that.relations.relationObjArray.members[i]);
				}
				this.relations.members[this.relations.members.length] = r;
				this.layer.add(r.group);
			}
		}

		// for(int i=0;i<that.rela)
		for( i = 0; i < this.relations.members.length; i++) {
			this.relations.members[i].update();
		}
	}
	this.hide = function() {
		var i = 0;

		for( i = 0; i < that.classes.length; i++) {
			that.classes[i].group.visible = false;
		}
		for( i = 0; i < that.relations.members.length; i++) {
			that.relations.members[i].group.visible = false;
		}
		that.backgroundRect.visible = false;
		that.buildingRelation.relationLine.visible = false;
		that.update();
	}

	this.show = function() {
		var i = 0;

		for( i = 0; i < that.classes.length; i++) {
			that.classes[i].group.visible = true;
		}
		for( i = 0; i < that.relations.members.length; i++) {
			that.relations.members[i].group.visible = true;
		}
		that.backgroundRect.visible = true;
		that.buildingRelation.relationLine.visible = false;
		that.layer.moveToTop();
		that.update();
	}
}